{
语法百题 解题报告：
 http://git.oschina.net/et0662/sznoi-grammar/ 
}
var
	a:array[1..100] of string;
	t,x,y:string;
	n,i,j,k,o:integer;
begin
	readln(n);
	for i:=1 to n do
		readln(a[i]);
	readln(o);
	while(o>0)do
	begin
		readln(t);
		i:=pos(' ',t);
		j:=length(t);
		x:=copy(t,1,i-1);
		y:=copy(t,i+1,j-i);
		for k:=1 to n do
		begin
			if(a[k]=x) then i:=k;
			if(a[k]=y) then j:=k;
		end;
		if i<j then
		begin
			for k:=i to j do
				if k<>j then
					write(a[k],' ')
				else
					writeln(a[k]);
		end
		else
		begin
			for k:=i downto j do
				if k<>j then
					write(a[k],' ')
				else
					writeln(a[k]);
		end;
		dec(o);
	end;
end.
